#[test]
fn dts() {
    use std::rc::Rc;
    let pointer_size = std::mem::size_of::<&u8>();
    println!("{}", pointer_size);
    assert_eq!(2 * pointer_size, std::mem::size_of::<&[u8]>());
    assert_eq!(2 * pointer_size, std::mem::size_of::<*const [u8]>());
    assert_eq!(2 * pointer_size, std::mem::size_of::<Box<[u8]>>());
    assert_eq!(2 * pointer_size, std::mem::size_of::<Rc<[u8]>>());
}


#[test]
fn test_create_slice() {
    let v = vec![1, 2, 3, 4, 5];
    let s = &v[1..3];
    assert_eq!(s, &[2, 3]);
    assert_eq!(s.len(), 2);

    let array = [1, 2, 3, 4, 5];
    let slice = &array[1..3];
    println!("{:?}", slice);

    assert_eq!(s[0], 2);
    assert_eq!(s[1], 3);
    assert_eq!(s.get(0), Some(&2));
}

#[test]
fn test_array_slice() {
    let mut array = [1, 2, 3, 4, 5];
    // let slice = &array[1..3];

    println!("{:?}", &array);
    println!("{:?}", &mut array);
}

#[test]
fn test_slice_align() {
    unsafe {
        // let bytes: [u8; 7] = [1, 2, 3, 4, 5, 6, 7];
        let mut bytes: [u8; 7] = [1, 2, 3, 4, 5, 6, 7];
        // let (prefix, shorts, suffix) = bytes.align_to::<u16>();
        let (prefix, shorts, suffix) = bytes.align_to_mut::<u16>();
        // less_efficient_algorithm_for_bytes(prefix);
        // more_efficient_algorithm_for_aligned_shorts(shorts);
        // less_efficient_algorithm_for_bytes(suffix);

        println!("prefix:{:?}", prefix);
        println!("shorts:{:?}", shorts);
        println!("suffix:{:?}", suffix);
    }
}

#[test]
fn as_chunks() {
    let slice = ['l', 'o', 'r', 'e', 'm'];
    // while let item = slice.chunks(3).next() {
    //     println!("{:?}", item);
    // }
    let mut iter = slice.rchunks(3);
    println!("{:?}", iter.next().unwrap());
    println!("{:?}", iter.next().unwrap());

    // #![feature(array_windows)]
    // let mut iter2 = slice.array_windows();

    println!("{:?}", iter.next().unwrap());
    unsafe {
        // println!("as_ascii = {:?}", slice.as_ascii());
    }

    // assert_eq!(iter.next().unwrap(), &['e', 'm']);
    // assert_eq!(iter.next().unwrap(), &['o', 'r']);
    // assert_eq!(iter.next().unwrap(), &['l']);
    // assert!(iter.next().is_none());
    let slice: &[char] = &['l', 'o', 'r', 'e', 'm', '!'];
    // let chunks: &[[char; 1]] =
    //     // SAFETY: 1 个元素的块永远不会剩余
    //     // unsafe { slice.as_chunks_unchecked() };
    // assert_eq!(chunks, &[['l'], ['o'], ['r'], ['e'], ['m'], ['!']]);
    // let chunks: &[[char; 3]] =
    //     // SAFETY: 切片长度 (6) 是 3 的倍数
    //     unsafe { slice.as_chunks_unchecked() };
    // assert_eq!(chunks, &[['l', 'o', 'r'], ['e', 'm', '!']]);
}

#[test]
fn as_ptr() {
    let x = &mut [1, 2, 4];
    let x_ptr = x.as_ptr();
    println!("{:p}", x_ptr);

    let x_ptr_mut = x.as_mut_ptr();
    unsafe {
        for i in 0..x.len() {
            *x_ptr_mut.add(i) += 3;
        }
    }
    println!("as_mut_ptr = {:?}", x);

    println!("as_ptr_range = {:?}", x.as_ptr_range());
    unsafe {
        for i in 0..x.len() {
            println!("{}: {}-> {}", i, x.get_unchecked(i), *x_ptr.add(i));
            assert_eq!(x.get_unchecked(i), &*x_ptr.add(i));
        }
    }
}

#[test]
fn slice_partition_point() {
    let mut s = vec![0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55];
    let num = 42;
    let idx = s.partition_point(|&x| x < num);
    println!("idx = {}", idx);
    s.insert(idx, num);
    assert_eq!(s, [0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 42, 55]);
}
